Show the code
library(maps)
library(viridis)
library(tidyverse)
library(leaflet)
library(sf)
library(tigris)
# Load the USArrests dataset
data(USArrests)
# Prepare the data for leaflet
arrests_data <- USArrests %>%
rownames_to_column(var = "state") %>%
mutate(state = tolower(state))
# Load the US states map data
us_states_map <- map_data("state")
# Define color palette
color_pal <- colorBin(viridis_pal()(5), domain = arrests_data$Murder, bins = 5)
# Join the US states map data with the arrests data
us_states_map_data <- us_states_map %>%
left_join(arrests_data, by = c("region" = "state"))
# Load the US states shapefile data
us_states_shapefile <- states(class = "sf", resolution = "20m") %>%
st_transform(4326) %>%
mutate(state = tolower(NAME))
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|== | 4%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|======= | 10%
|
|======== | 11%
|
|========= | 12%
|
|========= | 13%
|
|=========== | 16%
|
|============ | 17%
|
|============= | 18%
|
|============== | 20%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 23%
|
|=================== | 27%
|
|===================== | 30%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================== | 38%
|
|============================ | 40%
|
|============================= | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|================================ | 46%
|
|=================================== | 50%
|
|==================================== | 52%
|
|======================================= | 55%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================== | 60%
|
|=========================================== | 61%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|================================================ | 68%
|
|================================================= | 70%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 73%
|
|====================================================== | 77%
|
|======================================================= | 79%
|
|======================================================== | 79%
|
|======================================================== | 81%
|
|========================================================= | 82%
|
|========================================================== | 83%
|
|=========================================================== | 84%
|
|============================================================ | 85%
|
|============================================================= | 87%
|
|============================================================== | 88%
|
|=============================================================== | 90%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|======================================================================| 100%
Show the code
# Join the US states shapefile data with the arrests data
us_states_map_data <- us_states_shapefile %>%
left_join(arrests_data, by = c("state" = "state"))